﻿using Caliburn.Micro;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System.Collections.ObjectModel;
using Teach1.Gui.Models;

namespace Teach1.Gui.ViewModels
{
    public class MainViewModel: Conductor<object>
    {
        private double _EndDepth = 1500;
        private int _mode;

        public MainViewModel() 
        {
            CurveData.Add(new CurveDataItem() { No=1, Depth = 2843.0, Value = -999.25 });
            CurveData.Add(new CurveDataItem() { No=2, Depth = 2843.1, Value = -999.25 });
            CurveData.Add(new CurveDataItem() { No=3, Depth = 2843.2, Value = -999.25 });
            CreatePlotModel();
        }
        public ObservableCollection<CurveDataItem> CurveData { get; } = new();


        public void Apply()
        {
            EndDepth = 2000;
            CurveData.Add(new CurveDataItem{No = 4, Depth=2900, Value = 3.0});
        }
        public PlotModel? PlotModel { get; set; }

        private void CreatePlotModel()
        {
            var plot = new PlotModel { Title = "PP Raw Sonic Emw RT" };
            var random = new Random(42); // Fixed seed for reproducible results
            var data = new List<double>();

            // Simulate 1000 data points with normal distribution pattern
            for (var i = 0; i < 1000; i++)
            {
                // Generate data with mean around 75 and standard deviation of 15
                var value = random.NextGaussian(75, 15);
                // Ensure values are within realistic bounds (0-100)
                value = Math.Max(0, Math.Min(100, value));
                data.Add(value);
            }

            // Create histogram series
            var histogramSeries = new HistogramSeries
            {
                Title = "Frequency",
                FillColor = OxyColors.SkyBlue,
                StrokeColor = OxyColors.Black,
                StrokeThickness = 1
            };

            // Define bins for histogram
            int binCount = 20;
            double minValue = 0;
            double maxValue = 100;
            var binWidth = (maxValue - minValue) / binCount;

            var bins = new List<HistogramItem>();

            for (int i = 0; i < binCount; i++)
            {
                double binStart = minValue + i * binWidth;
                double binEnd = binStart + binWidth;

                var count = data.Count(x => x >= binStart && x < binEnd);
                // Make sure the last bin includes the maximum value
                if (i == binCount - 1)
                    count = data.Count(x => x >= binStart && x <= binEnd);

                bins.Add(new HistogramItem(binStart, binEnd, count, 0));
            }

            histogramSeries.Items.AddRange(bins);

            // Add axes
            plot.Axes.Add(new LinearAxis
            {
                Position = AxisPosition.Bottom,
                Title = "Value Range",
                Minimum = minValue,
                Maximum = maxValue
            });

            plot.Axes.Add(new LinearAxis
            {
                Position = AxisPosition.Left,
                Title = "Frequency",
                Minimum = 0
            });

            plot.Series.Add(histogramSeries);
            PlotModel = plot;
        }
        public double StartDepth { get; set; } = 500;
        public double EndDepth { 
            get => _EndDepth; 
            set => Set(ref _EndDepth ,value); 
        }
        public double DepthStep { get; set; } = 0.125;

        public int Mode
        {
            get => _mode;
            set => Set(ref _mode, value);
        }
    }
    
}
